package gitStudent;

import java.util.ArrayList;
import java.util.Scanner;

public class index02 {
    public static void main(String[] args) {
        //准备一个集合用于存放学生信息
        ArrayList<Student> list = new ArrayList<>();
        //无法判断主页会执行几次，选择死循环
        while(true) {
            //给出学生管理系统主页提示窗口
            System.out.println("-------------学生管理系统---------------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看所有学生");
            System.out.println("5.退出");
            System.out.println("啥也不是");
            //让用户自己输入想要做的选项
            System.out.println("请输入您要进行的操作");
            Scanner sc = new Scanner(System.in);
            int choice = sc.nextInt();
            //因为用户只会输入一个数值，跟据数值进行匹配对应的选项
            switch (choice){
                case 1:
                    //System.out.println("添加学生");
                    addStudent(list);
                    break;
                case 2:
                    //System.out.println("删除学生");
                    deleteStudent(list);
                    break;
                case 3:
                    //System.out.println("修改学生");
                    getStudent(list);
                    break;
                case 4:
                   // System.out.println("查看所有学生");
                    setStudent(list);
                    break;
                case 5:
                    System.out.println("感谢您的使用，再见！");
                    System.exit(0);
                default:
                    System.out.println("您输入的数值有误，请重新输入！");

            }
        }

    }
    /**
     * 定义方法，匹配id
     */
    public static  int  getId(String id , ArrayList<Student> list){
        // -1代表没有找到
        int index = -1;
        //遍历集合，与输入的id进行匹配，如果匹配成功返回索引值，不成功返回-1.
        for (int i = 0; i < list.size(); i++) {
            Student setId = list.get(i);
            if(setId.getId().equals(id)){
                index = i;
                break;
            }

        }
        return index;
    }
    /**
     * 修改学生信息
     */
    private static void getStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要修改学生的id");
        String getid = sc.next();
        int index = getId(getid,list);
        if(index == -1)
        {
            System.out.println("修改失败，您输入的id有误，请查验后再进行修改");
        }else{
            System.out.println("请输入要修改的学生姓名");
            String name = sc.next();
            System.out.println("请输入学生年龄:");
            int age = sc.nextInt();
            System.out.println("请输入学生出生日期");
            String birthday = sc.next();
            Student stu = new Student(getid,name,age,birthday);
            list.add(index,stu);
            System.out.println("修改成功！！");
        }
    }

    /**
     * 查看学生信息
     */
    private static void setStudent(ArrayList<Student> list) {
       if (list.size() != 0) {
           for (int i = 0; i < list.size(); i++) {
               Student stu = list.get(i);
               System.out.println("学号\t\t\t姓名\t\t年龄\t\t生日");
               System.out.println(stu.getId() + "\t\t" + stu.getName() + "\t\t" + stu.getAge() + "\t\t" + stu.getBirthday());
           }
       }
       else
       {
           System.out.println("查看失败，请添加后在进行查看");
       }
    }
    /**
     * 删除学生信息
     */
    private static void deleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要删除学生的id");
        String getid = sc.next();
        int index = getId(getid,list);
        if(index == -1)
        {
            System.out.println("修改失败，您输入的id有误，请查验后再进行删除");
        }else{
            list.remove(index);
            System.out.println("删除成功");
        }
    }

    /**
     * 添加学生信息
     */
    private static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生的id");
        String getid = sc.next();
        int index = getId(getid,list);
        if(index == -1) {
            System.out.println("请输入学生姓名：");
            String name = sc.next();
            System.out.println("请输入学生年龄:");
            int age = sc.nextInt();
            System.out.println("请输入学生出生日期：");
            String birthday = sc.next();
            Student stu = new Student(getid, name, age, birthday);
            list.add(stu);
            System.out.println("添加成功！！");
        }else{
            System.out.println("您输入的学号已存在请重新输入");
            System.out.println("啥也不是！！");
        }
    }


}
